package com.novoda.runbuddy.controller;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;

import com.novoda.runbuddy.activity.MapOfRuns;
import com.novoda.runbuddy.activity.Today;
import com.novoda.runbuddy.model.ListOfRuns;
import com.novoda.runbuddy.util.DBHelperImpl;
import com.novoda.runbuddy.util.IDBHelper;
import com.novoda.runbuddy.view.RunEntryView;
import com.novoda.runbuddy.view.TodayView;

public class DayControllerImpl implements IDayController {

	@SuppressWarnings("unused")
	private TodayView		todayView;
	@SuppressWarnings("unused")
	private RunEntryView	runEntryView;
	private IDBHelper		dbHelper;
	private Activity		context;
	private int				MAPOFJOG	= 3333;

	public DayControllerImpl() {
	}

	public DayControllerImpl(Activity today) {
		this.dbHelper = new DBHelperImpl(today);
		this.context = today;
	}

	public void createTodayView(Today today, TextView miles, TextView timeRun, ListOfRuns allRunsToday) {
		this.todayView = new TodayView(today, miles, timeRun, allRunsToday);
	}

	// public void createLastJogEntryView(Activity today, TextView miles,
	// TextView jogName_heading, TextView timeRun, long jogId) {
	// this.jogEntryView = new JogEntryView(today, miles, jogName_heading,
	// timeRun, jogId, this);
	// }

	public ListOfRuns getDayOfRuns(Integer day, Integer month, Integer year) {
		return (ListOfRuns) dbHelper.fetchAllJogEntries(day, month, year);
	}

	public ListOfRuns getTodaysRuns() {
		return (ListOfRuns) dbHelper.fetchAllTodaysRunEntries();
	}

	public Cursor getCursorOfAllRuns() {
		return dbHelper.getCursorOfAllRuns();
	}

	public void setDBHelper(IDBHelper dbHelper) {
		this.dbHelper = dbHelper;
	}

	public void openMapViewForRuns(long runStartTime) {
		Calendar cal = new GregorianCalendar();
		cal.setTimeInMillis(runStartTime);

		Intent intent = new Intent(context, MapOfRuns.class);
		Bundle bundle = new Bundle();
		bundle.putInt("day", cal.get(Calendar.DAY_OF_MONTH));
		bundle.putInt("month", cal.get(Calendar.MONTH) + 1);
		bundle.putInt("year", cal.get(Calendar.YEAR));
		intent.putExtras(bundle);
		context.startActivityForResult(intent, MAPOFJOG);
	}

}
